﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RH.Common.Extensions;
using RH.Domain;
using RH.Web.ViewModels;
using RH.Web.Utility;
using AutoMapper;
using RH.Web.ViewModels.UtilityModels;

namespace RH.Web.Areas.Site.Controllers
{
    [Authorize(Roles = "Company Admin,Company User,Site User")]
    public class FavoritesController : BaseController
    {
		private AppEntities _Context = new AppEntities();
        
		#region Constructors
		public FavoritesController()
		{
		}

		public FavoritesController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods

        /// <summary>
        /// Get List Of Product Added To Wish List
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }
        public ActionResult List()
        {   
            UtilityDAO dao = new UtilityDAO();
            DbParameter[] parameters = new[] {            
                new DbParameter("@UserId", LoggedInUser.ID),
            };
            System.Data.DataSet ds = dao.GetDataSet(DbObject.SP.GET_USER_FAVORITES, parameters, true);            
            ListConfig listConfig = new ListConfig(ds.Tables[0], null);
            return View(listConfig);
        }
        /// <summary>
        /// Marks a Favorite property as Deleted into the Database
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns>
        /// Returns:
        ///  -1 If the entity is not found in the databae
        ///  -2 If the favorite property is not belongs to the currently logged in user
        ///  +1 If everything is ok, then returs 1
        /// </returns>
        [HttpPost]
        public int Delete(int id)
        {
            UserWishList entity = _Context.UserWishLists.FirstOrDefault(P => P.ID == id);
            if (entity == null)
                return -1;
            if (entity.UserID != LoggedInUser.ID)
                return -2;

            entity.IsDeleted = true;
            //_Context.DeleteObject(entity);
            _Context.SaveChanges();

            return 1;
        }
		#endregion Action Methods
		
		#region Helper Methods

        #endregion			
    }
}
